Learn R Programming

fastR (version 1.1)

Covariance and correlation matrix: Fast covariance and correlation matrix calculation

Description

Fast covariance and correlation matrix calculation.

Usage

cova(x)
cora(x)

Arguments

x
A matrix with data. It has to be matrix, if it is data.frame for example the function does not turn it into a matrix.

Value

The covariance or correlation matrix.

Details

The calculations take place faster than the built-in functions covcor. We found this method in stackoverflow. This is true if the number of variables is high, say from 500 and above. For less variables, it's better to use the R's built-in function.

References

Shabalin, A.A. (2012). Matrix eQTL: Ultra fast eQTL analysis via large matrix operations. Bioinformatics 28(10): 1353-1358.

See Also

cor, cov

Examples

Run this code
x <-matrix( rnorm( 1000 * 500), ncol = 500 )
system.time( cor(x) )
system.time( cora(x) )
s1 <- cor(x)
s2 <- cora(x)
all.equal(s1, s2)


x <- as.matrix(iris[, 1:4])
system.time( for (i in 1:10000) cova(x) )
system.time( for (i in 1:10000) cov(x) )

Run the code above in your browser using DataLab